<template>
  <div
    class="body"
    v-loading="loading"
    element-loading-text="请先去认证或者去平台合作"
    :element-loading-spinner="svg"
    element-loading-svg-view-box="-10, -10, 50, 50"
    element-loading-background="rgba(122, 122, 122, 0.8)"
  >
    <h4>
      商品管理
      <el-button type="primary" style="margin-left: 16px" @click="zeng">
        新增
      </el-button>
    </h4>
    <el-table :data="commodity" style="width: 100%">
      <el-table-column prop="id" label="商品编号" width="150" />
      <el-table-column prop="name" label="商品名称" width="120" />
      <el-table-column prop="productType" label="商品类型" width="120" />
      <el-table-column prop="commodityPrice" label="价格" width="120" />
      <el-table-column prop="status" label="状态" width="120" />
      <el-table-column prop="sellingPoint" label="介绍" width="600" />
      <el-table-column fixed="right" label="操作" min-width="120">
        <template #default="scope">
          <el-button
            link
            type="primary"
            size="small"
            @click="delectsc(scope.row.id)"
          >
            删除
          </el-button>
          <el-button
            link
            type="primary"
            size="small"
            @click="xiugai(scope.row.id)"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 抽屉 -->
    <el-drawer v-model="drawer" :title="biaoti" :before-close="handleClose">
      <!--  抽屉内容 -->
      <el-form
        ref="ruleFormRef"
        :model="form"
        :rules="rules"
        label-width="auto"
        style="max-width: 600px"
      >
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="商品价格" prop="commodityPrice">
          <el-input v-model="form.commodityPrice" />
        </el-form-item>
        <el-form-item label="商品类型" prop="productType">
          <el-select v-model="form.productType" placeholder="请选择类型">
            <el-option
              v-for="c in flei"
              :key="c.id"
              :label="c.name"
              :value="c.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择是否上架">
            <el-option label="上架中" value="上架中" />
            <el-option label="未上架" value="未上架" />
            <el-option label="已下架" value="已下架" />
          </el-select>
        </el-form-item>
        <el-form-item label="商品介绍">
          <el-input v-model="form.sellingPoint" type="textarea" />
        </el-form-item>
        <el-form-item style="width: 100%; margin-left: 60px">
          <el-button type="primary" style="width: 40%" @click="saveorupdate"
            >提交</el-button
          >
          <el-button style="width: 40%" @click="drawer = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>


<script  setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import commodityjs from "@/api/merchant/commodity";

//导入缓存js
import { useTokenStore } from "@/stores/token";
import { toRaw } from '@vue/reactivity'
const yonghu = useTokenStore().token;

//遮罩层

const loading = ref(true);
const svg = `
        <path class="path" d="
          M 30 15
          L 28 17
          M 25.61 25.61
          A 15 15, 0, 0, 1, 15 30
          A 15 15, 0, 1, 1, 27.99 7.5
          L 15 15
        " style="stroke-width: 4px; fill: rgba(0, 0, 0, 0)"/>
      `;
const zezhao = () => {
  let sz=toRaw(yonghu.auditStatus)
  if(sz==1){
    loading.value=false;
  }else{
    loading.value=true
  }
};

//表单属性
const shangping = async (id) => {
  let data = await commodityjs.dingdanAll(id);
  commodity.value = data.shuju;
};
//表单验证
const ruleFormRef = ref(null);
const commodity = ref([]);
const rules = ref({
  name: [
    { required: true, message: "请输入商品名称", trigger: "blur" },
    { min: 2, max: 10, message: "长度在2-10", trigger: "blur" },
  ],
  status: [
    {
      required: true,
      message: "不允许为空",
      trigger: "blur",
    },
  ],
  commodityPrice: [{ required: true, message: "请输入价格", trigger: "blur" }],
  productType: [{ required: true, message: "请输入类型", trigger: "blur" }],
});
const saveorupdate = () => {
  ruleFormRef.value.validate((valid) => {
    if (valid) {
      // 说明校验通过
      // 可以向后端提交数据
      xiuorzeng();
    } else {
      Element.error("校验失败");
      return false;
    }
  });
};
//商品类型
const flei = ref([]);
const fenleic = async () => {
  let data = await commodityjs.fenlei();
  flei.value = data.shuju;
};
//抽屉
const drawer = ref(false);
const biaoti = ref("");
const form = ref({});
//抽屉方法
const zeng = () => {
  form.value = {};
  biaoti.value = "添加商品";
  fenleic();
  drawer.value = true;
};
const xiugai = (id) => {
  form.value = {};
  biaoti.value = "修改商品";
  fenleic();
  xiangqiang(id);
  drawer.value = true;
};
//查询详情
const xiangqiang = async (tid) => {
  let data = await commodityjs.xiangqing(tid);
  form.value = data.shuju;
};
//修改或新增
const xiuorzeng = async () => {
  form.value.merchantId = yonghu.id;
  let data = await commodityjs.gaiorzeng(form);
  if (data.flag) {
    alert("成功");
    shangping(yonghu.id);
    drawer.value = false;
  } else {
    alert("失败");
  }
};
//删除
const delectsc = async (tid) => {
  let data = commodityjs.delectx(tid);
  if (data.flag) {
    alert("删除成功");
    shangping(yonghu.id);
  } else {
    alert("删除失败");
  }
};
//钩子函数
onMounted(() => {
  zezhao()
  shangping(yonghu.id);
});
</script>


<style lang='scss' scoped>
.body {
  width: 100%;
  height: 97%;
  justify-content: center;
  align-items: center;
  font-family: "Montserrat", sans-serif;
  background-color: #ecf0f3;
  color: #a0a5a8;
  position: relative;
}
</style>